//
//  QRCodeService.m
//  SmartPharmacy
//
//  Created by czj on 15/12/25.
//  Copyright © 2015年 aniuge. All rights reserved.
//

#import "QRCodeService.h"


@implementation QRCodeService

- (void)beginGetQRCodeRequest:(NSString *)qrCode
{
    NSMutableDictionary *postDataDic = [[NSMutableDictionary alloc]init];
    
    
    [postDataDic setObject:qrCode forKey:@"qrcode"];
    
    NSString *url = [NSString stringWithFormat:@"%@/%@",kAppServerForHttp,@"HomePage/QRCode"];
    
    [JSTHttpClient sendRequest:url requestMethod:AFRequestMethodPost
                    parameters:postDataDic
                       success:^(id responseObject)
     {
         [self parseQRCodeInfoItem:responseObject];
     }
                       failure:^(NSUInteger statusCode, NSString *error)
     {
         [self getQRCodeInfoRequestOK:NO type:nil content:nil errorMsg:error];
     }];
    
}

- (void)parseQRCodeInfoItem:(id)responseObject
{
    NSNumber *number = [responseObject objectForKey:@"status"];
    
    if ([number isEqualToNumber:[NSNumber numberWithInt:1]])
    {
        NSDictionary *dataDic = [responseObject objectForKey:@"data"];
        
        NSNumber *typeNumer = [dataDic objectForKey:@"type"];
        NSString *content = [dataDic objectForKey:@"content"];
        [self getQRCodeInfoRequestOK:YES type:typeNumer content:content errorMsg:nil];
    }
    else
    {
        NSString *errorMsg = [responseObject objectForKey:@"msg"];
        [self getQRCodeInfoRequestOK:NO type:nil content:nil errorMsg:errorMsg];
    }
}

- (void)getQRCodeInfoRequestOK:(BOOL)isSuccess
                          type:(NSNumber *)type
                       content:(NSString *)content
                      errorMsg:(NSString *)errorMsg;
{
    if (_delegate && [_delegate respondsToSelector:@selector(getQRCodeInfoResult:type:content:errorMsg:)])
    {
        [_delegate getQRCodeInfoResult:isSuccess type:type content:content errorMsg:errorMsg];
    }
}

@end

